# -*- coding: utf-8 -*-
import os
import nuke


def selected_nodes():
    return nuke.selectedNodes()


def export_selected(path):
    _dir = os.path.dirname(path)
    if not os.path.isdir(_dir):
        os.makedirs(_dir)
    nuke.nodeCopy(path)


def nuke_import(path):
    nuke.nodePaste(path)


def save_as(path, overwrite=1):
    file_dir = os.path.dirname(path)
    if not os.path.isdir(file_dir):
        os.makedirs(file_dir)
    nuke.scriptSaveAs(path, overwrite=overwrite)


def open_file(file_name):
    nuke.scriptClose(ignoreUnsavedChanges=True)
    nuke.scriptOpen(file_name)


def replace_node(oldNode, newNode):
    for n in oldNode.dependent():
        for i in range(n.inputs()):
            if n.input(i) == oldNode:
                n.setInput(i, newNode)
    oldXPos = oldNode.xpos()
    oldYPos = oldNode.ypos()
    oldNode.setXYpos(newNode.xpos(), newNode.ypos())
    newNode.setXYpos(oldXPos, oldYPos)
